<?php

class RutaFirma {
	public $rute_firme_id; 	
	public $firma_id;
	public $loc1; 		
	public $loc2;
	public $version;
	public $status;	

	function __construct($firma_id = null, $loc1 = null, $loc2 = null) {
		$this->rute_firme_id = CT_NOT_SAVED_YET;
		$this->firma_id = $firma_id;
		$this->loc1 = $loc1;
		$this->loc2 = $loc2;
		$this->version = 0;
		$this->status = CT_RUTA_FIRMA_NEW;
	}
	
	public function queryInsertRutaFirma($db) {
		
		$query = $db->prepare("INSERT INTO `rute_firme`  ( 	`firma_id`, 		`loc1` , 
															`loc2`,				`version`
		                               				  ) 
    							VALUES  (					:firma_id,  		:loc1, 
		            										:loc2,   			:version		                      		
		                      			);");		
		
		$query->bindParam(':firma_id', 		$this->firma_id);
		$query->bindParam(':loc1', 			$this->loc1);
		$query->bindParam(':loc2', 			$this->loc2);
		$query->bindParam(':version', 		$this->version);
	
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new QueryInsertRutaFirmaException("Error inserting ruta firma " . $this->firma_id . $errInfo);
		}
		
  		$this->rute_firme_id = (int) $db->lastInsertId();		
  		$query->closeCursor();
		return $this->rute_firme_id; 				
	}
	
  	public static function &queryLoadRutaFirmaByID($db, $rute_firme_id) {
        $query = $db->prepare("SELECT *
                    			FROM rute_firme
                    			WHERE rute_firme_id = :rute_firme_id
                    			LIMIT 1");
                    			
        $query->bindParam(':rute_firme_id', $rute_firme_id);
                
        if ($query->execute() === FALSE) {

            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
        	$query->closeCursor();
        	throw new QueryLoadRutaFirmaException("Failed loading ruta firma with id " . $rute_firme_id . $errInfo);            
        }
            
        $data = $query->fetch(PDO::FETCH_ASSOC);
        
        $ruta = new RutaFirma($data['rute_firme_id'], $data['loc1'], $data['loc2']);
        $ruta->rute_firme_id = (int) $data['rute_firme_id'];
        $ruta->version = $data['version'];
        $query->closeCursor();
        return $ruta;
    }	
    
    function queryDeleteRutaFirma($db) {
      	$query = $db->prepare("DELETE FROM rute_firme WHERE rute_firme_id = :rute_firme_id");      	
      	$query->bindParam(':rute_firme_id', $this->rute_firme_id);
      	      	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeleteRutaFirmaException("Faild deleting ruta firma with id: " . $this->rute_firme_id . $errInfo);
      	}
      		
      	$query->closeCursor();
    	return CT_RESULT_OK;      	
    }    
    
    function queryUpdateRutaFirma($db) {
		$this->version++;

		$query = $db->prepare("UPDATE `rute_firme`  SET 	
													`firma_id` = :firma_id , 		`loc1` =  :loc1, 
									 			  	`loc2` = :loc2, 				`version` = :version	                        		 	                        		
	                            	WHERE rute_firme_id = :rute_firme_id
	                					AND version < :version");
		
		$query->bindParam(':firma_id', 		$this->firma_id);
		$query->bindParam(':loc1', 			$this->loc1);
		$query->bindParam(':loc2', 			$this->loc2);	
		$query->bindParam(':version',		$this->version);		
		$query->bindParam(':rute_firme_id',	$this->rute_firme_id);		
				               
		if ($query->execute() === FALSE) {			
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	        $query->closeCursor();	  
			throw new QueryUpdateRutaFirmaException("Failed updating ruta firma with id: " . $this->rute_firme_id . $errInfo);
		}
		 
		if ($query->rowCount() == 0) {

	        $query->closeCursor();
			throw new QueryWrongVersionException("Failed updating ruta firma. Reason: wrong version!");
		}
		
       //reset the status to unmodified for a possible re-edit;
	    
        $this->status = CT_RUTA_FIRMA_UNMODIFIED;		  	
		$query->closeCursor();
		
        return CT_RESULT_OK;    	
    }
    
    //deletes ruta firma by id; does not require an object.... STATIC
    
    public static function deleteRutaFirmaById($db, $rute_firme_id) {
    	$query = $db->prepare("DELETE FROM rute_firme WHERE rute_firme_id = :rute_firme_id LIMIT 1");
    	$query->bindParam(':rute_firme_id', $rute_firme_id);
    	    	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeleteRutaFirmaException("Failed deleting ruta firma with id: " . $id_contact . $errInfo);
      	}
      		      		
    	return CT_RESULT_OK;      	
    }
    
    public static function &queryLoadByFirma($db, $firma_id) {
    	
        $query = $db->prepare("SELECT *
                    			FROM rute_firme
                    			WHERE firma_id = :firma_id");        
        $query->bindParam(':firma_id', $firma_id);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
        	throw new QueryLoadRutaFirmaException("Failed loading ruta firma by firma; firma id = " . $id_firma . $errInfo);                    
        }
        
        $rute = array();
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {  
            $ruta = new RutaFirma($data['firma_id'], $data['loc1'], $data['loc2']);
            
            $ruta->rute_firme_id = $data['rute_firme_id'];
            $ruta->version = $data['version'];
            $ruta->status = CT_RUTA_FIRMA_UNMODIFIED;
            $rute[] = $ruta;
        }        

        $query->closeCursor();
        return $rute;
    }
    
    public static function deleteRutaFirmaByIdFirma($db, $firma_id) {
    	$query = $db->prepare("DELETE FROM rute_firme WHERE firma_id = :firma_id");
    	
    	$query->bindParam(':firma_id', $firma_id);
    	    	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeleteRutaFirmaException("Failed deleting ruta firma with id_firma: " . $firma_id . $errInfo);
      	}
      		      		
    	return CT_RESULT_OK;      	
    }
    
//    public static function queryLoadByClient($db, $conditie, $id_client, $per_pag, $pagina) {    	
//    	
//    	$offset = $per_pag * $pagina; 	
//    			    	
//        $query = "SELECT SQL_CALC_FOUND_ROWS contact.nume, contact.sex, contact.functie, contact.tel_fix, contact.tel_fax,
//        				contact.tel_mobil, contact.email, contact.version, contact.id
//						FROM contact
//						LEFT JOIN firme ON contact.id_firma = firme.id
//						WHERE firme.id_client = " . $id_client . "
//							AND $conditie
//						LIMIT $pagina, $per_pag";
//
//		//echo $query;						
//        if (($result = $db->query($query)) === FALSE) {
//            $errInfo = $query->errorInfo();
//            $errInfo = $errInfo[2];
//           	throw new QueryLoadContactException("Failed loading contact by client; contact id = " . $id_client . $errInfo);                    
//		} 
//		
//        $query2 = "SELECT FOUND_ROWS() AS total";
//
//        if (($result2 = $db->query($query2)) === FALSE) {			
//            $errInfo = $query->errorInfo();
//            $errInfo = $errInfo[2];
//           	throw new QueryLoadContactException("Failed loading contact by client; contact id = " . $id_client . $errInfo);                    
//		}         
//        $data2 = $result2->fetch(PDO::FETCH_ASSOC);
//        $total = $data2['total'];       
//                 	
//        $contacte = array();
//
//        while ($data = $result->fetch(PDO::FETCH_ASSOC)) {        	  
//            $contact = new Contact($data['id'], $data['nume'], $data['sex']
//                            , $data['functie'], $data['tel_fix'], $data['tel_fax'], $data['tel_mobil'], $data['email']);
//            $contact->id_contact = (int) $data['id'];
//            $contact->version = $data['version'];
//            $contact->status = CT_CONT_UNMODIFIED;
//            $contacte[] = $contact;
//        }        
//        $result = array();
////        var_dump($total);
//        $result[0] = $total;
//        $result[1] = $contacte;
//        return $result;    	
//    }
}

?>